﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml.Serialization;
using Common;

namespace Model
{
    /// <summary>
    /// 当游戏状态发生改变时
    /// </summary>
    /// <returns></returns>
    public delegate bool DataChanged();
    public class GameState
    {
        DateTime radio30Date;
        DateTime radio50Date;
        DateTime radio70Date;
        DateTime radio80Date;
        DateTime dailyQuestDate;
        DateTime expQuestDate;//循环经验任务完成的时间
        string accountInfo;
        bool radio30Completed = false;
        bool radio50Completed = false;
        bool radio70Completed = false;
        bool dailyQuestCompleted = false;
        bool radio80Completed = false;
        bool expQuestCompleted = false;

        /// <summary>
        /// 账户信息
        /// </summary>
        public string AccountInfo
        {
            get { return accountInfo; }
            set { accountInfo = value; }
        }

        /// <summary>
        /// 数据发生改变时引发
        /// </summary>
        public event DataChanged OnChanged;

        /// <summary>
        /// 进入30副本的日期
        /// </summary>
        public DateTime Radio30Date
        {
            get { return radio30Date; }
            set {
                if (value != radio30Date)
                {
                    radio30Date = value;
                    if (OnChanged != null)
                    {
                        OnChanged();
                    }
                }
            }
        }

        /// <summary>
        /// 进入50副本的日期
        /// </summary>
        public DateTime Radio50Date
        {
            get { return radio50Date; }
            set
            {
                if (value != radio50Date)
                {
                    radio50Date = value;
                    if (OnChanged != null)
                    {
                        OnChanged();
                    }
                }
            }
        }

        /// <summary>
        /// 进入70副本的日期
        /// </summary>
        public DateTime Radio70Date
        {
            get { return radio70Date; }
            set
            {
                if (value != radio70Date)
                {
                    radio70Date = value;
                    if (OnChanged != null)
                    {
                        OnChanged();
                    }
                }
            }
        }

        /// <summary>
        /// 进入80副本的日期
        /// </summary>
        public DateTime Radio80Date
        {
            get { return radio80Date; }
            set
            {
                if (value != radio80Date)
                {
                    radio80Date = value;
                    if (OnChanged != null)
                    {
                        OnChanged();
                    }
                }
            }
        }

        /// <summary>
        /// 老板娘日常任务的日期
        /// </summary>
        public DateTime DailyQuestDate
        {
            get { return dailyQuestDate; }
            set
            {
                if (value != dailyQuestDate)
                {
                    dailyQuestDate = value;
                    if (OnChanged != null)
                    {
                        OnChanged();
                    }
                }
            }
        }

        /// <summary>
        /// 循环经验任务的日期
        /// </summary>
        public DateTime ExpQuestDate
        {
            get { return expQuestDate; }
            set
            {
                if (value != expQuestDate)
                {
                    expQuestDate = value;
                    if (OnChanged != null)
                    {
                        OnChanged();
                    }
                }
            }
        }

        /// <summary>
        /// 30副本是否已完成
        /// </summary>
        public bool Radio30Completed
        {
            get { return radio30Completed; }
            set
            {
                if (value != radio30Completed)
                {
                    radio30Completed = value;
                    if (OnChanged != null)
                    {
                        OnChanged();
                    }
                }
            }
        }


        /// <summary>
        /// 50副本是否已完成
        /// </summary>
        public bool Radio50Completed
        {
            get { return radio50Completed; }
            set
            {
                if (value != radio50Completed)
                {
                    radio50Completed = value;
                    if (OnChanged != null)
                    {
                        OnChanged();
                    }
                }
            }
        }


        /// <summary>
        /// 70副本是否已完成
        /// </summary>
        public bool Radio70Completed
        {
            get { return radio70Completed; }
            set
            {
                if (value != radio70Completed)
                {
                    radio70Completed = value;
                    if (OnChanged != null)
                    {
                        OnChanged();
                    }
                }
            }
        }

 

        /// <summary>
        /// 老板娘日常任务是否已完成
        /// </summary>
        public bool DailyQuestCompleted
        {
            get { return dailyQuestCompleted; }
            set
            {
                if (value != dailyQuestCompleted)
                {
                    dailyQuestCompleted = value;
                    if (OnChanged != null)
                    {
                        OnChanged();
                    }
                }
            }
        }

        /// <summary>
        /// 循环经验任务是否已完成
        /// </summary>
        public bool ExpQuestCompleted
        {
            get { return expQuestCompleted; }
            set
            {
                if (value != expQuestCompleted)
                {
                    expQuestCompleted = value;
                    if (OnChanged != null)
                    {
                        OnChanged();
                    }
                }
            }
        }

        /// <summary>
        /// 80副本是否已完成
        /// </summary>
        public bool Radio80Completed
        {
            get { return radio80Completed; }
            set
            {
                if (value != radio80Completed)
                {
                    radio80Completed = value;
                    if (OnChanged != null)
                    {
                        OnChanged();
                    }
                }
            }
        }

        /// <summary>
        /// 构造一个GameState
        /// </summary>
        public GameState()
        {
        }

        /// <summary>
        /// 构造一个GameState
        /// </summary>
        public GameState(string accountInfo)
        {
            this.accountInfo = accountInfo;
            this.radio30Date = DateTime.Now;
            this.radio50Date = DateTime.Now;
            this.radio70Date = DateTime.Now;
            this.radio80Date = DateTime.Now;
            this.dailyQuestDate = DateTime.Now;
            this.expQuestDate = DateTime.Now;
        }

        /// <summary>
        /// 保存数据
        /// </summary>
        public bool Save()
        {
            string path = AppDomain.CurrentDomain.BaseDirectory + "//config//";
            try
            {
                using (FileStream stream = new FileStream(path + accountInfo + "_state.xml", FileMode.Create, FileAccess.ReadWrite, FileShare.Read))
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(GameState));
                    serializer.Serialize(stream, this);
                    stream.Close();
                }
                return true;
            }
            catch (Exception ex)
            {
                //日志记录
                Console.WriteLine("保存 " + accountInfo + " 的游戏状态信息时出错：");
                Tools.PrintException(ex);      
            }
            return false;
        }
    }
}
